package com.woniu.service.impl;

import com.woniu.entity.Supplier;
import com.woniu.mapper.ProductMapper;
import com.woniu.mapper.SupplierMapper;
import com.woniu.service.SupplierService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private ProductMapper productMapper;

    // 1. 获取所有供应商列表

    // 1. 获取所有供应商列表
    @Override
    public ResponseUtil getAllSuppliers() {
        try {
            List<Supplier> suppliers = supplierMapper.findAll();
            return ResponseUtil.success(ResponseEnum.OK,suppliers);
        } catch (Exception e) {
            return ResponseUtil.error("查询供应商列表失败：" + e.getMessage());
        }
    }

    // 2. 根据ID获取供应商详情
    @Override
    public ResponseUtil getSupplierById(Integer supplierId) {
        try {
            if (supplierId == null) {
                return ResponseUtil.error("供应商ID不能为空");
            }

            Supplier supplier = supplierMapper.findById(supplierId);
            if (supplier == null) {
                return ResponseUtil.error("供应商不存在");
            }
            return ResponseUtil.success(ResponseEnum.OK,supplier);
        } catch (Exception e) {
            return ResponseUtil.error("查询供应商详情失败：" + e.getMessage());
        }
    }

    // 3. 添加供应商
    @Override
    public ResponseUtil addSupplier(Supplier supplier) {
        try {
            // 表单验证
            if (supplier.getName() == null || supplier.getName().trim().isEmpty()) {
                return ResponseUtil.error("供应商名称不能为空");
            }
            if (supplier.getContact() == null || supplier.getContact().trim().isEmpty()) {
                return ResponseUtil.error("联系人不能为空");
            }
            if (supplier.getPhone() == null || supplier.getPhone().trim().isEmpty()) {
                return ResponseUtil.error("联系电话不能为空");
            }
            // 简单的手机号格式验证
            if (!supplier.getPhone().matches("^1[3-9]\\d{9}$")) {
                return ResponseUtil.error("手机号格式不正确");
            }

            supplier.setStatus(1); // 默认正常状态
            supplierMapper.add(supplier);
            return ResponseUtil.success(ResponseEnum.OK,supplier);
        } catch (Exception e) {
            return ResponseUtil.error("添加供应商失败：" + e.getMessage());
        }
    }

    // 4. 修改供应商
    @Override
    public ResponseUtil updateSupplier(Supplier supplier) {
        try {
            // 检查供应商是否存在
            if (supplier.getSupplierId() == null) {
                return ResponseUtil.error("供应商ID不能为空");
            }

            Supplier oldSupplier = supplierMapper.findById(supplier.getSupplierId());
            if (oldSupplier == null) {
                return ResponseUtil.error("供应商不存在");
            }

            // 表单验证
            if (supplier.getName() != null && supplier.getName().trim().isEmpty()) {
                return ResponseUtil.error("供应商名称不能为空");
            }
            if (supplier.getPhone() != null && !supplier.getPhone().matches("^1[3-9]\\d{9}$")) {
                return ResponseUtil.error("手机号格式不正确");
            }

            // 保存原名称用于关联更新
            String oldName = oldSupplier.getName();

            // 更新供应商信息
            if (supplier.getName() != null) {
                oldSupplier.setName(supplier.getName());
            }
            if (supplier.getRegionId() != null) {
                oldSupplier.setRegionId(supplier.getRegionId());
            }
            if (supplier.getContact() != null) {
                oldSupplier.setContact(supplier.getContact());
            }
            if (supplier.getPhone() != null) {
                oldSupplier.setPhone(supplier.getPhone());
            }
            if (supplier.getAddress() != null) {
                oldSupplier.setAddress(supplier.getAddress());
            }
            if (supplier.getStatus() != null) {
                oldSupplier.setStatus(supplier.getStatus());
            }

            supplierMapper.update(oldSupplier);



            return ResponseUtil.get(ResponseEnum.OK);
        } catch (Exception e) {
            return ResponseUtil.error("修改供应商失败：" + e.getMessage());
        }
    }

    // 5. 冻结/解冻供应商
    @Override
    public ResponseUtil freezeSupplier(Integer supplierId, Integer status) {
        try {
            // 参数校验
            if (supplierId == null) {
                return ResponseUtil.error("供应商ID不能为空");
            }
            if (status == null || (status != 0 && status != 1)) {
                return ResponseUtil.error("状态必须为0或1");
            }

            // 检查供应商是否存在
            Supplier supplier = supplierMapper.findById(supplierId);
            if (supplier == null) {
                return ResponseUtil.error("供应商不存在");
            }

            // 如果是冻结操作，检查是否有商品
            if (status == 0) {
                int productCount = supplierMapper.countProductsBySupplierId(supplierId);
                if (productCount > 0) {
                    return ResponseUtil.error("该供应商下存在商品，无法冻结");
                }
            }

            // 执行冻结/解冻
            supplier.setStatus(status);
            supplierMapper.update(supplier);
            return ResponseUtil.get(ResponseEnum.SUPPLIER_FREEZE_SUCCESS);
        } catch (Exception e) {
            return ResponseUtil.error("操作供应商状态失败：" + e.getMessage());
        }
    }

    // 6. 删除供应商
    @Override
    public ResponseUtil deleteSupplier(Integer supplierId) {
        try {
            // 参数校验
            if (supplierId == null) {
                return ResponseUtil.error("供应商ID不能为空");
            }

            // 检查供应商是否存在
            Supplier supplier = supplierMapper.findById(supplierId);
            if (supplier == null) {
                return ResponseUtil.error("供应商不存在");
            }

            // 检查是否有商品
            int productCount = supplierMapper.countProductsBySupplierId(supplierId);
            if (productCount > 0) {
                return ResponseUtil.error("该供应商下存在商品，无法删除");
            }

            // 执行删除
            supplierMapper.delete(supplierId);
            return ResponseUtil.get(ResponseEnum.SUPPLIER_DELETE_SUCCESS);
        } catch (Exception e) {
            return ResponseUtil.error("删除供应商失败：" + e.getMessage());
        }
    }

    // 7. 搜索供应商
    @Override
    public ResponseUtil searchSuppliers(String keyword) {
        try {
            List<Supplier> suppliers = supplierMapper.search(keyword);
            return ResponseUtil.success(ResponseEnum.OK,suppliers);
        } catch (Exception e) {
            return ResponseUtil.error("搜索供应商失败：" + e.getMessage());
        }
    }
}
